<?php

namespace app\common\Model;


use service\DataokeService;

class DtkCategory extends CommonModel{


    /**
     * 先清空在 插入
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public
    function sync_category_from_dataoke(){
        $ret = DataokeService::get_super_category();
        if(!$ret || $ret['code']!==0){
            return return_array(600, '获取数据异常', $ret);
        }
        self::where('1=1')->delete();
        $ok_count = 0;
        foreach($ret['data'] as $item){
            foreach($item['subcategories'] as $sub){
                $new_data = [
                    "cid"      => $item['cid'],
                    "cname"    => $item['cname'],
                    "cpic"     => $item['cpic'],
                    "subcid"   => $sub['subcid'],
                    "subcname" => $sub['subcname'],
                    "scpic"    => $sub['scpic'],
                ];
                if(self::create($new_data)){
                    $ok_count++;
                }
            }
        }
        return return_array(200, $ret['msg'], ['ok_count' => $ok_count]);
    }

    /**
     * 我的收藏
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public
    function get_collection($page_id){
        $ret = DataokeService::get_collection($page_id);
        if(!$ret || $ret['code']!==0){
            return return_array(600, '获取数据异常', $ret);
        }

        return return_array($ret);
    }

    /**
     *
     * 我发布的商品
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public
    function get_owner_goods($page_id){
        $ret = DataokeService::get_owner_goods($page_id);
        if(!$ret || $ret['code']!==0){
            return return_array(600, '获取数据异常', $ret);
        }
        return return_array($ret);
    }


}
